<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="文件名称" prop="fileOriginName">
        <el-input
          v-model="queryParams.fileOriginName"
          placeholder="请输入文件名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件类型" prop="fileSuffix">
        <el-input
          v-model="queryParams.fileSuffix"
          placeholder="例如txt、gif"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年级" prop="grade">
        <el-select v-model="queryParams.grade" style="width: 100%" placeholder="请选择" clearable >
          <el-option
            v-for="dict in dict.type.class_grade"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="内容关键字" prop="fileObjectName">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入搜索内容"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

    </el-form>

    <el-table v-loading="loading" :data="fileInfoList" @selection-change="handleSelectionChange">
      <el-table-column v-if="false" type="selection" width="55" align="center" />
      <!-- <el-table-column label="文件主键id" align="center" prop="fileId" /> -->
      <el-table-column label="图片预览" align="center" prop="pic" width="160">
        <template slot-scope="scope">
          <el-image
              style="width: 120px;height: 60px;"
              :src="scope.row.pic"
              lazy
              :preview-src-list="[scope.row.pic]">
            </el-image>
        </template>
      </el-table-column>
      <el-table-column label="文件编号" align="center" prop="fileId" width="80"/>
      <el-table-column label="文件名称" align="center" prop="fileOriginName"/>
      <el-table-column label="文件类型" align="center" prop="fileSuffix" width="80"/>
      <el-table-column label="内容关键字" align="center" prop="keyword"/>

      <el-table-column label="文件大小" align="center" prop="fileSizeInfo" width="100" />
      <el-table-column label="存储文件名称" v-if="false" align="center" prop="fileObjectName" />
      <el-table-column label="课件类型" align="center" width="80" prop="delFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.class_type" :value="scope.row.classType"/>
        </template>
      </el-table-column>
      <el-table-column label="年级" align="center" width="80" prop="grade">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.class_grade" :value="scope.row.grade"/>
        </template>
      </el-table-column>
      <el-table-column label="启用状态" align="center" width="80" prop="delFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.delFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="上传者" align="center" prop="createBy" width="60"/>
      <el-table-column label="上传时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleDownload(scope.row)"
          >下载</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="修改课件信息" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="90px">

        <el-form-item label="内容关键字" prop="keyword">
          <el-input  v-model="form.keyword" placeholder="请输入内容关键字" />
        </el-form-item>

        <el-form-item label="课件类型" prop="yesNo">
          <el-select v-model="form.classType" style="width: 100%" placeholder="请选择" clearable >
            <el-option
              v-for="dict in dict.type.class_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="年级" prop="grade">
          <el-select v-model="form.grade" style="width: 100%" placeholder="请选择" clearable >
            <el-option
              v-for="dict in dict.type.class_grade"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="是否启用" prop="yesNo">
          <el-select v-model="form.delFlag" style="width: 100%" placeholder="请选择" clearable >
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listClassFile, getFileInfo, updateFileInfo } from "@/api/cms/fileInfo";
import { getToken } from "@/utils/auth";
import image from '../../../assets/images/file.png'
import store from '@/store';
import studentAvatar from "@/views/system/user/profile/studentAvatar.vue";

export default {
  name: "FileInfo",
  dicts: ['sys_yes_no','class_type','class_grade'],
  components: {studentAvatar},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      fileNames: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文件管理表格数据
      fileInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 上传参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/upload",
        // 上传的文件列表
        fileList: []
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fileOriginName: null,
        fileSuffix: null,
        keyword: null,
        fileSizeInfo: null,
        fileObjectName: null,
        filePath: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fileOriginName: [
          { required: true, message: "文件名称不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "是否删除：Y-被删除，N-未删除不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 文件下载处理
    handleDownload(row) {
      const name = row.fileOriginName;
      const url = row.filePath;
      const suffix = url.substring(url.lastIndexOf("."), url.length);
      const a = document.createElement('a')
      a.setAttribute('download', name)
      a.setAttribute('target', '_blank')
      a.setAttribute('href', process.env.VUE_APP_BASE_API + url)
      a.click()
    },
    /** 查询文件管理列表 */
    getList() {
      this.loading = true;
      // console.log(this.queryParams);
      listClassFile(this.queryParams).then(response => {
        // console.log(response);
        for(let i = 0; i<response.rows.length; i++){
          let fileInfo = response.rows[i];
          switch (fileInfo.fileSuffix){
            case 'png': case 'jpg': case 'jpeg': case 'bmp': case 'gif':
              response.rows[i].pic = process.env.VUE_APP_BASE_API + fileInfo.filePath;
              break;
            default:
              response.rows[i].pic = image;
              break;
          }
        }
        this.fileInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fileId: null,
        fileOriginName: null,
        fileSuffix: null,
        fileSizeInfo: null,
        fileObjectName: null,
        filePath: null,
        keyword: null,
        delFlag: null,
        createBy: store.getters.name,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    viewPic(row){
      let picture = ""
      switch(row.fileSuffix){
        case 'png': case 'jpg': case 'jpeg': case 'bmp': case 'gif':
          picture = row.pic;
      }
      console.log(picture);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.keyword = undefined
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fileId)
      this.fileNames = selection.map(item => item.fileOriginName)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fileId = row.fileId
      getFileInfo(fileId).then(response => {
        this.form = response.data;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        console.log(this.form);
        updateFileInfo(this.form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      });
    },
  }
};
</script>
